<?php

class AuthComponent extends Object
{

    protected $status = array();
    protected $set_status = false;
    protected $controller;
    protected $type;
    protected $id;
    protected $data;

    function startup(&$controller)
    {
        $this->controller =& $controller;
        $this->_setStatus();
        $this->_setInfo();
    }

    function status()
    {
        if (! $this->set_status) {
            $this->_setStatus();
        }
        return $this->status;
    }

    function is($status)
    {
        return in_array($status, $this->status());
    }

    function Access($access)
    {
        if (! $this->Check($access)) {
            if ($this->is('guest')) {
                if (isset($this->controller->params['url']['url'])) {
					$url = '/'.$this->controller->params['url']['url'];
                    if ((strpos($url, '/login/') !== 0) && (strpos($url, '/logout/') !== 0) && (strpos($url, 'ajax_') === false) && ! $this->controller->Session->read('referer')) {
                        $this->controller->Session->write('referer', $url);
                    }
                }
                $this->controller->redirect('/login/', null, true);
            } else {
                $this->controller->_error(404);
            }
        } else {
            return true;
        }
    }

    function login($type, $id, $name = '', $redirect_url = '/')
    {
        $this->controller->Session->delete('user');
        $this->controller->Session->write('user', array('id'=>$id, 'type'=>$type, 'name'=>$name));
        $url = $this->controller->Session->read('referer');
        if (! strlen($url)) {
            $url = $redirect_url;
        }
        $this->controller->Session->delete('referer');
        $this->controller->redirect($url, null, true);
    }

    function name()
    {
		$tmp = $this->controller->Session->read('user');
    	return isset($tmp['name']) ? $tmp['name'] : '';
    }

    function logout()
    {
        if ($this->is('register')) {
        	$this->controller->Session->delete('user');
        }
        $this->controller->redirect('/', null, true);
    }

    function id()
    {
        return $this->id;
    }

    function type()
    {
        return $this->type;
    }

    function Check($access)
    {
        if (preg_match('/^[\\s]*$/s', $access)) {
            return false;
        }
        $res = false;
        $expr = preg_replace('/([^&|\\s!~()*]+|[*])/se', '("\\1"=="*" ? true : in_array("\\1", $this->status())) ? "true" : "false"', $access);
        $eval = '$res = (bool)('.$expr.');';
        eval($eval);
        return $res;
    }

    protected function _Auth()
    {
        $result = array();
        if ($tmp = $this->controller->Session->read('user')) {
        	$id = isset($tmp['id']) ? $tmp['id'] : 0;
        	$type = isset($tmp['type']) ? $tmp['type'] : 'client_admin';
            $model_name = 'ClientAdmin';
            if (! class_exists($model_name)) {
                loadModel($model_name);
            }
            $client_model = new $model_name();

            $client_model->rel();
            if ($user =& $client_model->getUser($id)) {
            	$data =& $user->data();
                $result[] = 'register';
                $result[] = $data['type'];
                $result[] = strtolower($data['status']);
            }
        }
        return $result;
    }

    protected function _setStatus()
    {
        if (count($a = $this->_Auth())) {
            $this->status = $a;
        } else {
            $this->status = array('guest');
        }
        $this->set_status = true;
    }

    protected function _setInfo()
    {
        if ($this->is('register')) {
            $tmp = $this->controller->Session->read('user');
            $this->id = isset($tmp['id']) ? $tmp['id'] : 0;
        }
    }

    public function &objForView()
    {
        return $this;
    }

}

?>